/*   _________  __  ____________  ___   ___  ____
 *  / ___/ __ \/  |/  /_  __/ _ \/ _ | / _ \/ __/
 * / /__/ /_/ / /|_/ / / / / , _/ __ |/ // / _/  
 * \___/\____/_/  /_/ /_/ /_/|_/_/ |_/____/___/  
 * COMTRADE file parser
 * -------------------------------------------------------------------------
 * Copyright (C) 2013 - Ericson Norberto A Santos (ericsonnorberto@gmail.com)
 * Federal University Of Campina Grande - UFCG
 * 
 * Supports on IEEE Std C37.111-1999 COMTRADE-99 and IEEE Std C37.111-1991
 * COMTRADE-91 standards.
 * 
 * ABSTRACT:
 * A common format for data files and exchange medium used for the interchange
 * of various types of fault, test, or simulation data for electrical power
 * systems is defined. Sources of transient data are described, and the case of
 * diskettes as an exchange medium is recommended.
 * Issues of sampling rates, filters, and sample rate conversions for transient
 * data being exchanged are discussed. Files for data exchange are specified,
 * as is the organization of the data. A sample file is given.
 * 
 * License information:
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
*/
#ifndef COMTRADE_PARSE_HPP
    #define COMTRADE_PARSE_HPP

#include "COMTRADE_defines.hpp"
#include "COMTRADE_structures.hpp"

class COMTRADE_parse
{
    public:
        COMTRADE_parse();
        virtual ~COMTRADE_parse();
        
        int COMTRADE_open_file(const string& filename);
        int COMTRADE_parse_cfg_file();
        
        /* Get general information */
        string COMTRADE_get_cfg_general_information(const int& data_id);
        
        /* Get a/d channel information */
        int    COMTRADE_get_cfg_ad_channel_information(const int& data_id);
        
        /* Get analogic channel information */
        double COMTRADE_get_cfg_analog_channel_d(int channel_id, int data_id);
        string COMTRADE_get_cfg_analog_channel_s(int channel_id, int data_id);
        int    COMTRADE_get_cfg_analog_channel_i(int channel_id, int data_id);
        
        
        /* Get digital channel information */
        string COMTRADE_get_cfg_digital_channel(int channel_id, int data_id);
        int    COMTRADE_get_cfg_digital_channel_status(int channel_id);
        
        /* Get line frequency information */
        double COMTRADE_get_cfg_line_frequency();
        
        /* Get sampling rate information */
        double COMTRADE_get_cfg_sampling_rate();
        int    COMTRADE_get_cfg_sampling_rate_data(int data_id);
        
        /* Get time stamp information */
        double COMTRADE_get_cfg_time_stamp(int data_id);
        
        /* Get file type information */
        string COMTRADE_get_cfg_file_type();
        
        /* Get time multiplier information */
        double COMTRADE_get_cfg_time_multiplier();
        
        
        /* DAT file parsing */
        int    COMTRADE_parse_dat_file();
        
        double COMTRADE_get_dat_analog_channel_sample(int sample, int channel, int multiplied);
        int    COMTRADE_get_dat_digital_channel_sample(int sample, int channel);
        int    COMTRADE_get_dat_total_records();
        int    COMTRADE_get_dat_size_of_file();
        int    COMTRADE_get_dat_time_stamp(int sample);
        
        
        
        void comtrade_teste();
        
        
    private:
        /* */
        bool COMTRADE_file_exists(const string& filename);
        
        /* */
        int  COMTRADE_parse_ascii_data();
        int  COMTRADE_parse_binary_data();
        
        
        /* */
        bool COMTRADE_check_bit_msb(const int& number);
        
        
        
        
    private:
        vector<comtrade_file_list> file_list;
        ifstream file_stream[4];
        
        /* Configuration file data */
        cfg_data_file configuration_file;
        vector<dat_data_file> data_file;
        
        
        
        
        /* Flags of parsed files */
        bool cfg_parse_file;
        bool dat_parse_file;
        bool hdr_parse_file;
        bool inf_parse_file;
        
        
        /* Data file constants */
        int dat_total_records;
        int dat_size_of_file;
        
        
        

};
#endif
/* COMTRADE_PARSE_HPP */